package com.atguigu.flink.chapter11;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/9/24 10:10
 */
public class Flink05_Sql_BaseUse {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
        
        DataStreamSource<WaterSensor> waterSensorStream =
            env.fromElements(new WaterSensor("sensor_1", 1000L, 10),
                             new WaterSensor("sensor_1", 2000L, 20),
                             new WaterSensor("sensor_2", 3000L, 30),
                             new WaterSensor("sensor_1", 4000L, 40),
                             new WaterSensor("sensor_1", 5000L, 50),
                             new WaterSensor("sensor_2", 6000L, 60));
        
        Table table = tenv.fromDataStream(waterSensorStream);
        
        // 1. 查询未注册的表
        //tenv.executeSql("..."); // 执行ddl语句和增删改(dml)
//        Table result = tenv.sqlQuery("select * from " + table + " where id='sensor_1'");// 执行查询
//        result.execute().print();
    
        // 2. 查询已经注册的表
        tenv.createTemporaryView("sensor", table); // 给table对象注册一个表名
    
        tenv.sqlQuery("select * from sensor where id='sensor_1'").execute().print();
        
    }
}
